package com.javakc.pms.dispord.controller;

import com.javakc.commonutils.api.APICODE;
import com.javakc.pms.dispord.entity.DispOrd;
import com.javakc.pms.dispord.service.DispOrdService;
import com.javakc.pms.dispord.vo.DispOrdQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * program:
 *
 * @author: zhangchenghui
 * @date: 2021/9/14
 * apiNote
 */
@Api(tags = "调度指令库 - 表现层")
@RestController
@RequestMapping("/pms/dispord")
@CrossOrigin
public class DispOrdController {

    @Autowired
    private DispOrdService dispOrdService;

    @ApiOperation("查询所有数据 - 调度指令库")
    @GetMapping
    public APICODE findAll(){
        //int i = 1/0;  异常试验
        List<DispOrd> list =  dispOrdService.findAll();
        return APICODE.OK().data("list",list);
    }

    @ApiOperation("带条件的分页查询 - 调度指令库")
    @PostMapping("findPage/{pageNo}/{pageSize}")
    public APICODE findPage(@RequestBody(required = false)
                            @ApiParam(name = "dispOrdQuery",value = "封装查询对象") DispOrdQuery dispOrdQuery,
                            @PathVariable("pageNo") @ApiParam(name = "pageNo",value = "当前页",required = true) int pageNo,
                            @PathVariable("pageSize") @ApiParam(name = "pageSize",value = "每页最大条数",required = true) int pageSize){
    Page<DispOrd> page = dispOrdService.findPage(dispOrdQuery, pageNo, pageSize);
    List<DispOrd> list = page.getContent();
    long totalElements = page.getTotalElements();
    return APICODE.OK().data("items",list).data("total",totalElements);
    }

    @ApiOperation("新增数据 - 调度指令库")
    @PostMapping("createDispOrd")
    public APICODE createDispOrd(@RequestBody DispOrd dispOrd){
        dispOrdService.saveOrUpdate(dispOrd);
        return APICODE.OK();
    }

    @ApiOperation("根据id获取数据 - 调度指令库")
    @GetMapping("{dispOrdId}")
    public APICODE view(@PathVariable("dispOrdId") int dispOrdId){
        DispOrd dispOrd = dispOrdService.getById(dispOrdId);
        return APICODE.OK().data("dispOrd",dispOrd);
    }

    @ApiOperation("修改数据 - 调度指令库")
    @PutMapping
    public APICODE update(DispOrd dispOrd){
        dispOrdService.saveOrUpdate(dispOrd);
        return APICODE.OK();
    }

    @ApiOperation("根据id删除 - 调度指令库")
    @DeleteMapping("{dispOrdId}")
    public APICODE deleteDispOrd(int dispOrdId){
        dispOrdService.removeById(dispOrdId);
        return APICODE.OK();
    }

}
